import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  {
    path:'/login',
    name:'login',
    component:() => import('@/views/Login.vue'),
    meta:{
      title: '登录',
    }
  },
  {
    path:'/register',
    name:'register',
    component:() => import('@/views/Register.vue'),
    meta:{
      title: '注册',
    }
  },
  {
    path:'/page',
    name: 'page',
    component: () => import ('@/views/Page.vue'),
    meta:{
      title: '主页',
    },
    children: [
      
      {
        path:'mine',
        name: 'mine',
        component: () => import ('../views/page/Mine.vue'),
      },
      {
        path:'intent',
        name: 'intent',
        component: () => import ('../views/page/Intent.vue'),
      },
      {
        path:'day',
        name: 'day',
        component: () => import ('../views/page/Day.vue'),
      },
      {
        path:'all',
        name: 'all',
        component: () => import ('../views/page/All.vue'),
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (!whiteList.includes(to.path)) {
    if (!sessionStorage.getItem('userInfo')) {
      router.push('/login')
      return
    }
    next()
    return
  }
  next()
})

export default router
